<script setup lang="ts">
  import { isExternal } from '@/utils/validate.ts'

  const props = defineProps({
    /*icon的名字*/
    iconClass: {
      type: String,
      required: true,
    },
    className: {
      type: String,
      default: '',
    },
  })
  const isExternal2 = () => {
    return isExternal(props.iconClass)
  }
  const iconName = () => {
    return `#icon-${props.iconClass}`
  }
  const svgClass = () => {
    if (props.className) {
      return 'svg-icon ' + props.className
    } else {
      return 'svg-icon'
    }
  }
  const styleExternalIcon = () => {
    return {
      mask: `url(${props.iconClass}) no-repeat 50% 50%`,
      '-webkit-mask': `url(${props.iconClass}) no-repeat 50% 50%`,
    }
  }
</script>

<script lang="ts">
  export default {
    name: 'IconSvg',
  }
</script>

<template>
  <div v-if="isExternal2()" :style="styleExternalIcon()" class="svg-external-icon svg-icon"></div>
  <svg v-else :class="svgClass()" aria-hidden="true">
    <use :xlink:href="iconName()" />
  </svg>
</template>

<style scoped lang="scss">
  .svg-icon {
    width: 1em;
    height: 1em;
    vertical-align: -0.15em;
    fill: currentColor;
    overflow: hidden;
  }

  .svg-external-icon {
    background-color: currentColor;
    mask-size: cover !important;
    display: inline-block;
  }
</style>
